<?php

namespace app\controller;


use app\service\UserWishService;
use support\Response;
use think\Exception;
use think\facade\Db;

//用户星愿雕像控制器
class UserWishController extends Base
{

    /**
     * 获取星愿雕像信息
     */
    public function index(): Response
    {
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserWishService())->get_user_wish($this->user_id);
            Db::commit();
        }  catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }
        return $this->json($res);
    }


    //刷新任务
    public function refresh(): Response
    {
        $wish_id = $this->request->post('wish_id','0');
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserWishService())->refresh($this->user_id,$wish_id);
            Db::commit();
        }  catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }
        return $this->json($res);
    }

    //完成任务
    public function complete(): Response
    {

        $wish_id = $this->request->post('wish_id','0');
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserWishService())->complete($this->user_id,$wish_id);
            Db::commit();
        }  catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }
        return $this->json($res);

    }


}